import java.util.Arrays;

class Test{

    //先合并两个数组
    public static void merge(int[] nums1,int[] nums2) {

        //int m = nums1.length-1;
        //int n = nums2.length-1;
        //for (int i = 0; i != n; ++i) {
       //     nums1[m + i] = nums2[i];
       // }
       /* for (int i = 0; i < nums1.length ; i++) {
            if(nums1[i] == 0){
                System.arraycopy(nums1,i+1,nums1,i,nums1.length-i-1);
                i--;
            }
        }
        for (int i = 0; i < nums2.length ; i++) {
            if(nums2[i] == 0){
                System.arraycopy(nums2,i+1,nums2,i,nums2.length-i-1);
                i--;
            }
        }*/
        int [] c = Arrays.copyOf(nums1,nums1.length+nums2.length);
        System.arraycopy(nums2,0,c,nums1.length,nums2.length);
        //再归并排序
        mergeSort(c);
        System.out.println(Arrays.toString(c));

    }
    public static   void mergeSort(int[] array) {
        mergeSortFun(array,0,array.length-1);

    }

    public static void mergeSortFun(int[] array,int left,int right) {
        if(left >= right) {// >
            return;
        }
        int mid = (right+left) / 2;
        mergeSortFun(array,left,mid);
        mergeSortFun(array,mid+1,right);
        //合并
        merge1(array,left,mid,right);
    }
    private static void merge1(int[] array,int left,
                              int mid,int right) {
        int[] tmp = new int[right-left+1];
        int k = 0;
        int s1 = left;
        int e1 = mid;
        int s2 = mid+1;
        int e2 = right;
        while (s1 <= e1 && s2 <= e2) {
            if(array[s1] <= array[s2]) {
                tmp[k++] = array[s1++];
            }else {
                tmp[k++] = array[s2++];
            }
        }
        while (s1 <= e1) {
            tmp[k++] = array[s1++];
        }
        while (s2 <= e2) {
            tmp[k++] = array[s2++];
        }
        //走到这里 相当于tmp数组 当中 所有的元素 都有序了
        //接下来把tmp数组的内容 拷贝到array数组当中
        for (int i = 0; i < k; i++) {
            array[i+left] = tmp[i];
        }
    }

    public static void main(String[] args) {
        int[] nums1 = new int[]{23,4,5,6,7};
        int[] nums2 = new int[]{24,55,6,0,1};
        merge(nums1,nums2);


    }


}